package com.tianqu.tms.www.transfer.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.transfer.actionform.CheckConfirmActionForm;
import com.tianqu.tms.www.transfer.viewhelper.CheckConfirmViewHelper;

public class PNCSearchResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(PNCSearchResultAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		CheckConfirmActionForm inputForm = (CheckConfirmActionForm) form;
		CheckConfirmViewHelper view = new CheckConfirmViewHelper();
		String serial = inputForm.getSerial();
		if (serial == null || "".equals(serial)) {
			setErrorsToRequest(request, TmsConstants.IM006, null);
			return mapping.getInputForward();
		}
		Codes code = Codes.getInstance();
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		accept = accept.getAcceptInfoBySerialAndCarLicense(serial, "", 0);
		if (accept != null) {
			if (accept.getStatus() == 16) {
				setErrorsToRequest(request, TmsConstants.IM006, null);
				return mapping.getInputForward();
			}
			view.setSerial(accept.getSerial());
			view.setStatus(code.getItemValueByItemIdAndItemCode(
					Codes.ACCEPT_STATUS, accept.getStatus()));
			view.setLicensePlate(accept.getLicensePlate());
			view.setVin(accept.getVin());
			view.setCarOwner(accept.getCarOwner());
			if (accept.getLz_printnum() > 0) {
				view.setLz_printnum(String.valueOf(accept.getLz_printnum()));
			} else {
				view.setLz_printnum("");
			}
			User user = new User();
			user = user.getUserInfoById(accept.getLz_printUserId());
			if (user != null) {
				view.setLz_printUserId(user.getRealName());
			} else {
				view.setLz_printUserId("");
			}
			if (accept.getLz_printTime() != null
					&& !"".equals(accept.getLz_printTime())) {
				view.setLz_printTime(DateUtil.DateToString11(accept
						.getLz_printTime())
						+ "  -  "
						+ (DateUtil.getATDay(accept.getLz_printTime())));
			} else {
				view.setLz_printTime("");
			}
			TransferProcessHistory his = new TransferProcessHistory();
			List list = his.loadtHistoryBySerial(serial);
			view.setLcList(list);
			if (accept.getMarketType() == 1) {
				Car car = Car.getInstance();
				car = car.findCarBySerial(accept.getBasicCarId());
				String vendorCode = "";
				String modelCode = "";
				if(car.getVendorCode()==null)
				{
					vendorCode = "";
				}
				if(car.getModelCode() == null)
				{
					modelCode = "";
				}
				view.setModeCode(vendorCode + modelCode);
				view.setRegisterNo(car.getRegisterCardNo());
				view.setCy_cardType(code.getItemValueByItemIdAndItemCode(
						Codes.VEHICLE_TYPE, car.getCarType()));
				if (car.getCarOwnerIdCardNo() != null
						&& !"".equals(car.getCarOwnerIdCardNo())) {
					view.setSfOrDmNo(car.getCarOwnerIdCardNo());
				} else {
					view.setSfOrDmNo(car.getOwnerOrgNo());
				}
				view.setContectPhone(car.getOwnerPhone());
			}
			if (accept.getMarketType() == 2) {
				TransferPersonalCarInfo person = new TransferPersonalCarInfo();
				person = person.getPersonalCarInfoBySerial(accept
						.getBasicCarId());
				String vendorCode = "";
				String modelCode = "";
				if(person.getVendorCode()==null)
				{
					vendorCode = "";
				}
				if(person.getModelCode() == null)
				{
					modelCode = "";
				}
				view.setModeCode(vendorCode + modelCode);
				view.setRegisterNo(person.getRegisterCardNo());
				view.setCy_cardType(code.getItemValueByItemIdAndItemCode(
						Codes.VEHICLE_TYPE, person.getCarType()));
				if (person.getCarOwnerIdCardNo() != null
						&& !"".equals(person.getCarOwnerIdCardNo())) {
					view.setSfOrDmNo(person.getCarOwnerIdCardNo());
				} else {
					view.setSfOrDmNo(person.getOwnerOrgNo());
				}
				view.setContectPhone(person.getOwnerPhone());
			}
		} else {
			setErrorsToRequest(request, TmsConstants.IM006, null);
			return mapping.getInputForward();
		}
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
